package drumkit;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Aleksey
 * Date: 02.01.2008
 * Time: 18:01:14
 */
public class Drumkit {
    public static final String DEFAULT_POSITION_NAME = "Default";

    private Document document;
    private String name;
    private ArrayList<Drum> drums;

    public Drumkit(String fileName) throws ParserConfigurationException, IOException, SAXException {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        document = documentBuilder.parse(new File(fileName));
        drums = new ArrayList<Drum>();
        parseDocument();
    }

    public Integer getDrumCount() {
        return drums.size();
    }

    public Drum getDrum(Integer index) {
        return drums.get(index);
    }

    public String getName() {
        return name;
    }

    private void parseDocument() {
        Integer drumCount = 0;
        Node node = document.getElementsByTagName("drumkit").item(0);
        NodeList nodeList = node.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            if (nodeList.item(i).getNodeName().equalsIgnoreCase("name")) {
                name = nodeList.item(i).getTextContent();
            }
            else
            if (nodeList.item(i).getNodeName().equalsIgnoreCase("drum")) {
                Drum newDrum = new Drum();
                Node anotherNode = nodeList.item(i);
                NodeList list = anotherNode.getChildNodes();
                for (int j = 0; j < list.getLength(); j++) {
                    if (list.item(j).getNodeName().equalsIgnoreCase("name")) {
                        newDrum.setName(list.item(j).getTextContent());
                        newDrum.setIndex(drumCount++);
                    }
                    else
                    if (list.item(j).getNodeName().equalsIgnoreCase("type")) {
                        newDrum.setType(list.item(j).getTextContent().equalsIgnoreCase("cymbals") ? Drum.DRUM_TYPE_CYMBALS :
                                list.item(j).getTextContent().equalsIgnoreCase("drums") ? Drum.DRUM_TYPE_DRUMS : Drum.DRUM_TYPE_UNKNOWN);
                    }
                    else
                    if (list.item(j).getNodeName().equalsIgnoreCase("position")) {
                        PositionedDrum positionedDrum = new PositionedDrum();
                        Node anotherNode2 = list.item(j);
                        NodeList list2 = anotherNode2.getChildNodes();
                        for (int k = 0; k < list2.getLength(); k++) {
                            if (list2.item(k).getNodeName().equalsIgnoreCase("name")) {
                                if (list2.item(k).getTextContent().equalsIgnoreCase(DEFAULT_POSITION_NAME)) {
                                    positionedDrum.setName("");
                                }
                                else {
                                    positionedDrum.setName(list2.item(k).getTextContent());
                                }
                            }
                            else
                            if (list2.item(k).getNodeName().equalsIgnoreCase("style")) {
                                PlayingStyle playingStyle = new PlayingStyle();
                                Node anotherNode3 = list2.item(k);
                                NodeList list3 = anotherNode3.getChildNodes();
                                for (int q = 0; q < list3.getLength(); q++) {
                                    if (list3.item(q).getNodeName().equalsIgnoreCase("name")) {
                                        playingStyle.setName(list3.item(q).getTextContent());

                                    }
                                    else
                                    if (list3.item(q).getNodeName().equalsIgnoreCase("key")) {
                                        playingStyle.addKey(Integer.parseInt(list3.item(q).getTextContent()));
                                    }
                                    else
                                    if (list3.item(q).getNodeName().equalsIgnoreCase("note")) {
                                        playingStyle.setNote(list3.item(q).getTextContent());
                                    }
                                }
                                positionedDrum.addStyle(playingStyle);
                            }
                        }
                        newDrum.addPosition(positionedDrum);
                    }
                }
                drums.add(newDrum);
            }
        }
    }
}
